package sword_offer;

import common.ListNode;

/**
 * @ClassName _24ReverseListNode
 * @Description: 反转链表
 * @Author: yongliang.ma
 * @Create_time:2022/10/9 13:30
 */
public class _24ReverseListNode {

    public static void main(String[] args) {
        ListNode _1node = new ListNode(1);
        ListNode _2node = new ListNode(2);
        ListNode _3node = new ListNode(3);
        _1node.next = _2node;
        _2node.next = _3node;

        ListNode listNode = new _24ReverseListNode().reverseList(_1node);
        System.out.println(listNode.val);
    }

    // 1、遍历节点使用栈存储起来，然后依次弹出构建节点数据

    // 2、一边遍历一边构建节点数据

    public ListNode reverseList(ListNode head){
        ListNode cursor = head, target = null, temp = null;
        while (cursor != null){
            temp = cursor;
            cursor = cursor.next;

            temp.next = target;
            target = temp;
        }

        return target;
    }

}
